package specimensManagement;

import java.util.ArrayList;

import javax.persistence.*;

import loansManagement.Loan;
import mediasManagement.Media;
import exceptions.LentMediaException;

import booksManagement.Book;


/*
 * Class representing a specimen of a book
 * @version 0.0.1
 */

/**
 * Entity implementation class for Entity: Specimen
 *
 */
@Entity
public class Specimen{

	// Fields
	@Id
	@Column(name="id_specimen",unique = true)
	private String specimenId;

	/**
	 * @uml.property  name="media"
	 * @uml.associationEnd  inverse="exemplaries:mediasManagement.Media"
	 */
	@ManyToOne
	@JoinColumn(name="media_fk")
	private Media media;

	/**
	 * @uml.property   name="currentLoan"
	 * @uml.associationEnd   inverse="specimen:loansManagement.Loan"
	 */
	@OneToOne(fetch = FetchType.EAGER, mappedBy = "pk.specimen")
	private Loan currentLoan;

	/**
	 * Getter of the property <tt>specId</tt>
	 * @return  Returns the specId.
	 * @uml.property  name="specId"
	 */
	public String getSpecimenId() {
		return specimenId;
	}

	/**
	 * Setter of the property <tt>specId</tt>
	 * @param specId  The specId to set.
	 * @uml.property  name="specId"
	 */
	public void setSpecimenId(String specId) {
		this.specimenId = specId;
	}

		
		/**
		 */
	public Specimen(Media media){
			this.media=media;
			this.specimenId=media.getId()+"_"+media.getNbCreatedSpecimen();
		}
	
	public Specimen(String specId,Media media){
			this.media=media;
			this.specimenId=specId+"_"+media.getNbCreatedSpecimen();
		}
	public Specimen(){
		
	}
		
	@Override
	public boolean equals(Object specimen) {
		if ( specimen == null)
			return false;
		
		Specimen ex = (Specimen) specimen;
		if(ex.getSpecimenId().equals(specimenId))
			return true;
		
		return false;
		}


	public boolean isLent() {
		return (currentLoan !=null);
		
	}


	public void lend(Loan loan) throws LentMediaException {
		if (this.isLent())
			throw new LentMediaException("Media already lent.");

		this.currentLoan=loan;
		this.loans.add(loan);
	}

	public void returnMedia() {
	}

	/**
	 * Getter of the property <tt>media</tt>
	 * @return  Returns the media.
	 * @uml.property  name="media"
	 */
	public Media getMedia() {
		return media;
	}

	/**
	 * Setter of the property <tt>media</tt>
	 * @param media  The media to set.
	 * @uml.property  name="media"
	 */
	public void setMedia(Media media) {
		this.media = media;
	}

	/**
	 * Getter of the property <tt>currentLoan</tt>
	 * @return  Returns the currentLoan.
	 * @uml.property  name="currentLoan"
	 */
	
	public Loan getCurrentLoan() {
		return currentLoan;
	}

	/**
	 * Setter of the property <tt>currentLoan</tt>
	 * @param currentLoan  The currentLoan to set.
	 * @uml.property  name="currentLoan"
	 */
	public void setCurrentLoan(Loan currentLoan) {
		this.currentLoan = currentLoan;
	}

	/**
	 * @uml.property   name="loans"
	 * @uml.associationEnd   multiplicity="(0 -1)" ordering="true" inverse="specimen1:loansManagement.Loan"
	 */
	@Transient
	private ArrayList loans = new ArrayList<Loan>();

	/**
	 * Getter of the property <tt>loans</tt>
	 * @return  Returns the loans.
	 * @uml.property  name="loans"
	 */
	public ArrayList getLoans() {
		return loans;
	}

	/**
	 * Setter of the property <tt>loans</tt>
	 * @param loans  The loans to set.
	 * @uml.property  name="loans"
	 */
	public void setLoans(ArrayList loans) {
		this.loans = loans;
	}

	@Override
	public String toString(){
		return "the specimen "+specimenId+" of "+media.getTitle();
		
		
		
	}
		
}
